//
//  ColPaginatorController.swift
//  jocool
//
//  Created by tong on 16/6/17.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Kingfisher
import Alamofire
open class ColPaginatorController: ColsController,Paginator {
    
    

    public var page_load_section = "section0"
    open override func viewDidLoad() {
        super.viewDidLoad()
        let vself = self
        vself.pagingScrollView = self.collectionView
    }
    
    open override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    open func pageRequest()
    {
        
        self.paginator.page = self.paginator.page + 1
        if paginator.page == PageConfig.page_begin
        {
            self.pagingScrollView.mj_footer?.endRefreshingWithNoMoreData()
        }
        
        if self.paginator.in_test
        {
            self.testPageRequest()
        }
        else
        {
            self.netPageRequest()
        }
    }

    
    //取到的数据的处理
    open func check(items:[Any])
    {
        
    }
 
    open override func mo_request_alert
        (
        msg:String = "请检查网络链接是否正常".loc,
        title:String = "数据链接失败".loc,
        bt_title:String = "重试".loc
        )
    {
        weak var wself = self
            self.loadingV.handle(message: msg, title: title, button: bt_title, handAction: {
            wself?.loadingV.loading()
            wself?.loadMore(page: (wself?.paginator.page_begin ?? PageConfig.page_begin))

//            wself?.refresh()
        })
    }

    open override func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        
    }
    
    
//    open override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        
//    }
    
    public var collectionAlwaysReload = false

    open func overseverListLoad(page:Int,sectionList:NSMutableArray)
    {
        if page == self.paginator.page_begin
        {
            sectionList.removeAllObjects()
            loadingV.dismiss()
        }
    }

    
    open func loadIndexPaths(indexPaths:[IndexPath],page:Int){
        if collectionAlwaysReload{
            collectionView.reloadData()
        }else{
            if page == self.paginator.page_begin
            {
                collectionView.reloadData()
            }
            else
            {
                collectionView.insertItems(at: indexPaths)
            }
        }
    }
    
    open func load(page: Int, succeed: Bool, list: [Any], response: RootData?, status: PageDataStatus, scrollView: UIScrollView) {
        
        if let leb = self.backMsgView as? UILabel
        {
            leb.text = ""
        }

        if let tb_view = scrollView as? UICollectionView
        {
            if succeed
            {
                if let items = tb_view.model[obj:self.page_load_section,nil] as? NSMutableArray
                {
                    self.overseverListLoad(page: page, sectionList: items)
                    self.check(items: list)
                    var count = items.count
                    var ps = [IndexPath]()
                    let section = page_section
                    for _ in list
                    {
                        let v = IndexPath(item: count, section: section)
                        ps.append(v)
                        count = count + 1
                    }
                    items.addObjects(from: list)
                    switch status{
                    case .empty:
                        if let leb = self.backMsgView as? UILabel
                        {
                            leb.text = self.paginator.empty_msg
                        }
                    case .err:
                        break
                    default:
                        break
                    }
                    loadIndexPaths(indexPaths: ps, page: page)
                }
            }
            else{
                
                if page == self.paginator.page_begin
                {
                    
                    
                    let msg = "请检查网络链接是否正常".loc
                    if let _ = response
                    {
                        self.mo_request_alert(msg: "请检查网络链接是否正常".loc)
                    }
                    else{
                        self.loadingV.showing = false
                        self.mo_request_alert(msg: msg)
//                        if let leb = self.backMsgView as? UILabel
//                        {
//                            _ = leb.ui.font17.cl_888
//                            leb.text = msg
//                        }
                        
                    }
                }

            }

            
        }
        
    }
    
    open var page_section:Int{
        get{
            let sectionS = self.page_load_section.replacingOccurrences(of: "section", with: "")

            return Int(sectionS) ?? 0
        }
    }
    
    deinit {
        self.paginator.net.cancel()
        
    }
    

}



